/**
 * @author Kristian Mandrup
 * @date June 10, 2009
 * @version 0.1
 * 
 * m3gen :: NodeObjBranchBuilder
 * 
 * - Builds a branch containing nested arrays of nodeObj
 * - these will be used to create the final jsTreeNode branch
 * 
 * Usage 
 * -----
 * var myNodeObjBranchBuilder = new NodeObjBranchBuilder();
 * var branchNodeObjects = myNodeObjBranchBuilder.execute(jsTreeNode, branchDefinition);
 */

/**
 * Constructor
 */
m3gen.NodeObjBranchBuilder = function(createOptions) {
	this.init(createOptions);
}

// inherit from Builder
// m3gen.NodeObjBranchBuilder.inherits(m3gen.BranchUtil);
m3gen.NodeObjBranchBuilder.inherits(m3gen.Base);

m3gen.NodeObjBranchBuilder.prototype.init = function(createOptions){
	this.setClassName('NodeObjBranchBuilder');		

	// call Builder superclass initializer 
	this.uber('init', createOptions);
}


/*
branchDef with languages:
        branchDef: {
            id: 'boys',
            languageData: {
				en: {
					title: 'My boys'
				},
				dk: {
					title: 'Mine drenge'
				},
				'default': {
					icon: 'folder.png'
				}
			},				
            branchType: 'folder'
        },

branchTypeRule with languages:
 
        'folder': {
			languageData: {
				en: {
					icon: 'folder_en.png'
				},
				dk: {
					icon: 'folder_dk.png'
				},
				'default': {
					icon: 'folder.png'
				}
			},            
            state: 'closed'
        }


simple branchTypeRule:

        'folder': {
            icon: 'folder.png',
            state: 'closed'
        }


simple branchDef:
 
        branchDef: {
            id: 'boys',
            title: 'My boys',
            branchType: 'folder'
        },

 */


 
/**
 * Creates a single branch nodeObj (used later for creating a branch jsTreeNode)
 * 
 * Called by ChildrenParser.parseBranch
 * - branchBuilder.execute(branchDefinition)
 * 
 * FLOW:
 * - get branchTypeRule from branchDefinition
 * - build resolvedContent_branch from branchDef and branchTypeRule
 * - build the branchNodeObj using the resolvedContent_branch
 * - 
 * 
 * @param 	{Object} branchDefinition	- must be a valid branchDef
 *
 * branchDefinition = { id: 'boys', title: 'My boys', branchType: 'folder'},
 * 
 * @return 	{Object} branch nodeObj
 * 
 * 
 */

/**
 * doExecute default empty wrapper
 * @param {Object} options
 */
m3gen.NodeObjBranchBuilder.prototype.execute = function(options){
	return this.doExecute(options);
}

m3gen.NodeObjBranchBuilder.prototype.doExecute = function(branchDefinition){	
	if (this.isValidObj(branchDefinition)) {
		// start with empty branch node	
		var branchNodeObj = null;
		// get branchType of branchDefinition
		var branchTypeRule = this.getBranchTypeRule_from_BranchDefinition(branchDefinition);
		// build resolvedContent_branch from branchDef and branchTypeRule 			
		var resolvedContent_branch = this.getResolvedContentBuilderInstance().buildFromBranch(branchDefinition, branchTypeRule)
		
		if (this.isValidObj(resolvedContent_branch)) {			
			// build the branchNodeObj using the resolvedContent_branch
			branchNodeObj = this.getNodeObjBuilderInstance().execute(resolvedContent_branch);
			branchNodeObj.isBranch = true;
		}		
		return branchNodeObj;
	} else this.handle(m3gen.msg.InvalidBranchDefinition);
}

